package com.xixiake.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.xixiake.application.BaseApplication;

/**
 *
 * Created by c-wanghengli on 2017/5/29.
 */
public class SharePerferenceUtil {

    private static SharedPreferences sp;
    private static SharedPreferences.Editor ed;

    /**
     * sharePerferenceUtil文件名称
     */
    private static final String SP_NAME = Constant.XXKCONFIG;

    private static final int MODE_PRIVATE = Context.MODE_PRIVATE;


    public static void putString(String key,String value){
        if(sp == null)
            sp = BaseApplication.getContext().getSharedPreferences(SP_NAME,MODE_PRIVATE);
        sp.edit().putString(key,value).commit();
    }


    public static String getString(String key,String defValue){

        if(sp == null)
            sp = BaseApplication.getContext().getSharedPreferences(SP_NAME,MODE_PRIVATE);
        return sp.getString(key,defValue);
    }


    public static void removeString(String key) {
        if (sp == null)
            sp = BaseApplication.getContext().getSharedPreferences(SP_NAME, MODE_PRIVATE);
        sp.edit().remove(key).commit();
    }

    public static void putInt(String key, int value) {
        if (sp == null)
            sp = BaseApplication.getContext().getSharedPreferences(SP_NAME, MODE_PRIVATE);
        sp.edit().putInt(key, value).commit();
    }

    public static int getInt(String key, int defValue) {
        if (sp == null)
            sp = BaseApplication.getContext().getSharedPreferences(SP_NAME, MODE_PRIVATE);
        return sp.getInt(key, defValue);
    }

    public static void putLong(String key, long value) {
        if (sp == null)
            sp = BaseApplication.getContext().getSharedPreferences(SP_NAME, MODE_PRIVATE);
        sp.edit().putLong(key, value).commit();
    }

    public static Long getLong(String key, long defValue) {
        if (sp == null)
            sp = BaseApplication.getContext().getSharedPreferences(SP_NAME, MODE_PRIVATE);
        return sp.getLong(key, defValue);
    }

    public static void putBoolean(String key, boolean value) {
        if (sp == null) {
            sp = BaseApplication.getContext().getSharedPreferences(SP_NAME, MODE_PRIVATE);
        }
        sp.edit().putBoolean(key, value).commit();
    }

    // WelcomeActivity报空指针的针对处理
    public static void putBooleanSpec(Context context, String key, boolean value) {
        if (sp == null) {
            sp = context.getSharedPreferences(SP_NAME, MODE_PRIVATE);
        }
        sp.edit().putBoolean(key, value).commit();
    }

    public static boolean getBoolean(String key, boolean defValue) {
        if (sp == null)
            sp = BaseApplication.getContext().getSharedPreferences(SP_NAME, MODE_PRIVATE);
        return sp.getBoolean(key, defValue);
    }
}
